<<<<<<< HEAD:lectures/lecture-12a/12-copilot.html QTM 350 - Data Science Computing

QTM 350 - Data Science Computing

Lecture 13 - AI-Assisted Programming with GitHub Copilot (and More)

Danilo Freire

Emory University

Hello, everyone!
Hope all is well! 😊

Lecture overview 📚

In this lecture, we will:

  • Discuss the pros and cons of AI-assisted programming with GitHub Copilot
  • Learn how to install Copilot in the command line
  • Show some useful features of Copilot, including:
    • Code generation
    • Code explanations
    • Use of @ extensions for specific tasks (new feature!)
    • Optimisation or simplification of code
    • Run tests and examples
    • Learn how to use agent mode to automate tasks

Installing GitHub Copilot in the command line 🖥️

Step 1: Install GitHub CLI

  • To use GitHub Copilot in the CLI, you need:
    • An active GitHub Copilot subscription
    • GitHub CLI installed
    • The Copilot CLI extension
  • MacOS and Linux users can install GitHub CLI with Homebrew:
brew install gh
  • Windows users can install GitHub CLI with Chocolatey:
choco install gh
gh extension install github/copilot-cli

Step 2: Authenticate with GitHub

  • To authenticate with GitHub, run:
gh auth login
  • Follow the instructions to authenticate with GitHub. Select GitHub.com
❯ gh auth login
? Where do you use GitHub?  [Use arrows to move, type to filter]
> GitHub.com
  Other
  • You will be asked to open a browser and authenticate with GitHub. Select HTTPS
❯ gh auth login
? Where do you use GitHub? GitHub.com
? What is your preferred protocol for Git operations on this host?  [Use arrows to move, type to filter]
> HTTPS
  SSH

Step 2: Authenticate with GitHub

? Authenticate Git with your GitHub credentials? (Y/n) Y
  • You will be asked to login
? How would you like to authenticate GitHub CLI?  [Use arrows to move, type to filter]
> Login with a web browser
  Paste an authentication token
  • Copy the code and press Enter to go to the browser. Press Continue, paste the code, and click on Authorize
! First copy your one-time code: 1111-AAAA
Press Enter to open github.com in your browser... 
  • If successful, you will see:
✓ Authentication complete.
- gh config set -h github.com git_protocol https
✓ Configured git protocol
✓ Logged in as danilofreire
  • And you’re done! 🎉

Step 3: Use Copilot in the CLI

gh extension install github/gh-copilot
  • And you should be ready to go! 🚀
  • GitHub Copilot in the CLI offers two primary commands
    • Explain Command: Use gh copilot explain followed by a command to get an explanation
    • Suggest Command: Use gh copilot suggest followed by a prompt to generate code

Step 3: Use Copilot in the CLI

  • You can also create aliases for these commands. For example:
    • ghce for gh copilot explain
    • ghcs for gh copilot suggest
  • If you use zsh, you can just copy the commands below in your terminal:
echo 'eval "$(gh copilot alias -- zsh)"' >> ~/.zshrc && source ~/.zshrc
  • Or if you use bash:
echo 'eval "$(gh copilot alias -- bash)"' >> ~/.bashrc && source ~/.bashrc
  • Windows PowerShell users can use:
$GH_COPILOT_PROFILE = Join-Path -Path $(Split-Path -Path $PROFILE -Parent) -ChildPath "gh-copilot.ps1"
gh copilot alias -- pwsh | Out-File ( New-Item -Path $GH_COPILOT_PROFILE -Force )
echo ". `"$GH_COPILOT_PROFILE`"" >> $PROFILE
  • Let’s see some examples!

Copilot in action! 🚀

Example 01: Stop struggling with terminal commands 🖥️

  • Let’s start with gh copilot explain to get an explanation of a command
  • The command below searches for log files and then searches within them for the word error
    • find / -name '*.log' -exec grep 'error' {} +

Example 01: Stop struggling with terminal commands 🖥️

  • Now let’s use gh copilot suggest to generate a command
  • ghcs "create three txt files with names test1.txt, test2.txt, and test3.txt", then add the content "Hello, world!" to each file, then list the files, show the content of each file, and delete them"

Example 01: Stop struggling with terminal commands 🖥️

Example 02: Stop struggling with Git 🐙

  • Let’s see how Copilot can help with Git commands
  • ghcs "create a new branch called 'feature-branch', switch to it, create a new file called 'feature.txt', add some content to it, commit the changes, push the branch to the remote repository, and open a pull request"

Example 03: Stop struggling with GitHub CLI 🐙

  • ghcs "how can I use gh to create a new private repository on GitHub danilofreire/testing, clone it to my computer, add a README file, commit the changes, push the changes to the remote repository, and open a pull request?"

Cool, isn’t it? 🤩

Let’s see what Copilot can do for you in VS Code

  • If you have installed Copilot in VS Code, just click on the Copilot icon (a chat bubble) in the bottom left corner
  • You can then type a prompt and see the suggestions. As you know how to ask it to generate code (same as in ChatGPT), let’s see other things that Copilot can do for you
  • Type /help to see the available commands
  • You will also note that there are commands such as @workspace, @vscode, @terminal, and @github that allow you to interact with the workspace, VS Code, the terminal, and GitHub (including your account), respectively
  • You can also interact with files with #file
  • Let’s have a look at some examples

Example 04: Explain code 🤔

  • Type /explain followed by a code snippet to get an explanation. You can select the snippet directly in the terminal, then just type /explain

Example 05: Run tests 🧪

  • Type /tests followed by a code snippet to run tests
  • Let’s see how it tests this function:
class SimpleCalculator:
    def add(self, a, b):
        return a + b

    def subtract(self, a, b):
        return a - b

    def multiply(self, a, b):
        return a * b

    def divide(self, a, b):
        if b == 0:
            raise ValueError("Cannot divide by zero")
        return a / b

    def square_root(self, a):
        if a < 0:
            raise ValueError("Cannot calculate square root of a negative number")
        return a ** 0.5

Example 05: Run tests 🧪

Example 06: Fix code 🛠️

  • Type /fix followed by a code snippet to fix it
  • This function is supposed to check if a number is even, but it has a logical error. The division operation (number / 2) should be replaced with the modulo operation (number % 2)
  • If you hoover over the Fixed Code block, you will see the options:
    • Apply the fix in the editor
    • Insert the fixed code at the cursor
    • Copy the fixed code to the clipboard
    • Insert into a terminal
    • Insert into a new file
def is_even(number):
    return number / 2 == 0

Example 06: Fix code 🛠

Example 07: Create new file and generate text/code 🚀

  • Type /new and pass the information about the file you want to create
  • /new create a file.txt file with the first three lines of Homer's Odyssey

Example 08: Run code 🏃‍♂️

  • You can use the @terminal command to write and run code in the terminal. Click on the terminal icon to insert the code
  • @terminal create a file titled test.py and add print("Hello, world!") using the command line

Example 09: Interact with GitHub 🐙

  • You can use the @github command to interact with GitHub
  • For example, let’s see what are the latest 3 commits of this repository (danilofreire/qtm350)
  • You can also use @github to create a new repository, open a pull request, and more

Example 10: Interact with VS Code 🚀

  • You can use the @vscode command to interact with VS Code
  • For example, let’s see how to open a new file in the editor

Example 11: Interact with the workspace 💻

  • The @workspace command allows you to interact with the workspace, which are the files and folders you have open in VS Code
  • For example, let’s ask it to list the files in the workspace

Example 11: Interact with the workspace 💻

  • Another example: let’s ask it to find files that contains bash commands
  • Click on the icons and they will open!

Last Example: Searching for text in files 📄

  • You can use the #file command to search for text in files
  • Combine it with the @workspace command to search in all files in the workspace
  • Let’s search for Python code in this file

Summary

  • GitHub Copilot can save you a lot of time and make you more productive (and it’s fun!)
  • It is not perfect, but it is a good tool as it is 🤓
  • You should also have a look at other AI coding tools, and I recommend you to try Cline and DeepSeek, as they work really well and can do much more than just code generation
  • Also, please read more about AI agents and how they can be used in your projects
  • I hope you will make good use of AI coding tools from now on 🤖
  • Find out other uses for it, test the other tools available, and let me know what you think!

And that’s it for today! 🎉

Thank you for your attention and see you all soon!

======= QTM 350 - Data Science Computing

QTM 350 - Data Science Computing

Lecture 15 - AI-Assisted Programming, APIs, and Agents (Continued)

Danilo Freire

Emory University

Nice to see you all again!
How are you all doing? 😊

Tavily discount code

LLM APIs 🧑🏻‍💻

LLM APIs

  • Application Programming Interfaces (APIs) are a way to interact with a model using code
  • They allow you to send a prompt to a model and get a response back
  • In contrast with user interfaces, APIs are made for computers
    • They connects computers or pieces of software to each other
  • APIs can do a lot of things, but we mostly associated with web APIs, which allow computers to interact with websites or servers on the internet
  • We can do the same with LLMs too!
  • Instead of going to a website, we can use an API straight from software

Source: Cloud Now

Roo Code

Documentation: https://docs.roocode.com/

Setting up Roo Code

  • First, install the Roo Code extension in VSCode
  • Open the extension (rocket icon 🚀) and let’s configure our first LLM!
  • Click on the gear (top right) and you will be taken to the settings
  • You will see a series of options, let’s see one by one
  • … but we need API keys!
  • In most cases, API credits are usually cheaper than monthly subscriptions for the average user
  • You can get them directly from the model’s website, or from OpenRouter
  • The good thing is, OpenRouter has many free models available too!

Adding API keys

  • OpenRouter provides a uniform way to interact with many APIs
  • And it is super easy to use: just sign up and get your API key
  • Then, we are doing to select the models we will use in Roo Code settings
  • For this example, we will use the free version of DeepSeek
  • Feel free to test other free models (or paid ones) available in OpenRouter
  • Go to https://openrouter.ai/ and sign up for an account (it is free, no credit card or subscription needed)

Adding API keys

  • Then, click on your picture and Keys
  • Copy the key and paste it in the Roo Code settings
    • You will only see the key once, so make sure to save it somewhere safe!
  • You can create as many keys as you want, and revoke them at any time
  • In Credit Limit, you can put a limit on how much you want to spend
  • Feel free to put $0.00 if you want to use only the free models
  • More models here: https://openrouter.ai/models

Adding API keys

  • Now, let’s go back to Roo Code settings and add the API key
  • Just copy and paste the key in the OpenRouter API Key field
  • We are almost ready to go!
  • The last step is to select the models we want to use
  • Model choice depends on the task you have at hand, but a general model is a good start
  • Note that here we are not running the models locally, but running them on (someone else’s) server
    • So while you gain speed, you lose privacy

Adding API keys

Running a model in Roo Code

Using agents in Roo Code

  • So far, we have seen how to use Roo Code to interact with APIs
  • It already saves you a lot of time, as you don’t have to go to a website to get the information you need
  • But what if you could automate even more tasks?
  • That’s where agents come in!
  • Agents are like chatbots that can act autonomously, including running code, sending emails, and interacting with websites
  • This is the future of AI, and it is already here!
  • For coders, one of the best uses of agents is to let it write and run code for you
  • It is the closest thing to “vibes programming” (😂) we have so far

Using agents in Roo Code

  • Let’s ask Roo Code to do the following:
    • Create a new folder called document
    • Create a new Quarto file called api.qmd
    • Write a few paragraphs about the importance of APIs
    • Save the file
    • Render the file and open it in VSCode
    • We could ask it to send it to GitHub, but we will do it manually just for the sake of it
  • Prompt:
    • Create a new folder called document in my current folder. Create a new Quarto file called api.qmd. Write three paragraphs about the importance of APIs. Save the file. Render the file and open it in VSCode.
  • And see the magic happen! 🪄

Using agents in Roo Code

Output

Completely automated! 🤖

Cool, right? 🤓

Alternatives to Roo Code and DeepSeek

  • Roo Code is not the only tool that allows you to interact with APIs
  • Other popular choices are Cline and Continue
  • They are all free to use, and you can choose the one that best fits your needs
    • Roo Code is actually a fork of Cline, but with more features
    • Continue is a new tool that is gaining popularity
  • They perform similar tasks and have a similar interface, so feel free to try them all 😉

Alternatives to Roo Code and DeepSeek

  • DeepSeek is not the only free model available in OpenRouter
  • Two models that are worth mentioning are Google Gemini and Qwen
  • Gemini offers free access to all its models, including their “thinking” ones
  • Now that you know how to use APIs, you can get an API key for free at https://aistudio.google.com/ and use it in VS Code
  • Qwen is a model specialised in coding, and it is great for writing code snippets
  • An advantage that both have over DeepSeek is that they are multimodal, meaning they can generate text, images, and code
  • For instance, you can input a screenshot on Gemini and ask it to transcribe and a translate a text, or Qwen to recreate a table in Markdown

Giving internet access to your editor 🌐

Browser-use

  • We have seen how to use LLMs locally, how to interact with APIs, and how to create agents
  • But what if you could browse the internet straight from your code editor?
  • OpenAI’s Operator was released to much fanfare and it allows you to do just that
  • But there is a free and open source alternative called Browser-use
  • It is a Python library that uses LLM APIs to interact with websites, extract information, and perform tasks
  • You can use it to scrape data from websites, fill out forms, send emails, and much more

Browser-use Web-ui

  • To make things more convenient, Browser-use comes with a web interface that allows you to interact with websites visually
  • In this case, we will need a model with vision capabilities, such as Google Gemini or Anthropic’s Claude
  • Let’s get an API from Google and test it in Browser-use!
  • But first, we need to install the software
  • You can find the instructions here

Browser-use Web-ui

Instructions

Step 1: Clone the Repository

git clone https://github.com/browser-use/web-ui.git
cd web-ui

Step 2: Set Up Python Environment They recommend using uv. Here: https://docs.astral.sh/uv/getting-started/installation/

curl -LsSf https://astral.sh/uv/install.sh | sh 
# pip install uv
uv venv --python 3.11

Activate the virtual environment:

  • Windows (Command Prompt):
.venv\Scripts\activate
  • Windows (PowerShell):
.\.venv\Scripts\Activate.ps1
  • macOS/Linux:
source .venv/bin/activate

Step 3: Install Dependencies Install Python packages:

uv pip install -r requirements.txt

Install Playwright:

playwright install --with-deps

Step 4: Configure Environment Create a copy of the example environment file:

  • Windows (Command Prompt):
copy .env.example .env
  • macOS/Linux/Windows (PowerShell):
cp .env.example .env
  1. Open .env in your preferred text editor and add your API keys and other settings

How to create a Google API key

  • It’s super easy to create a Google API key! You can do it in a few steps:
  • Go to the Google Cloud Console and sign in with your Google account
  • Create a new project or select an existing one (you can name it whatever you want)
  • Now, go to https://aistudio.google.com/apikey and click on “Get API Key” and “Create API Key”
  • Select the project you created or selected earlier
  • Copy the API key and paste it in the .env file in the GOOGLE_API_KEY field
  • Check if you are using the Free Plan (no credit card needed)
  • And that’s it! You are ready to use Google Gemini in Browser-use! 🤖

How to create a Google API key

https://console.cloud.google.com/

How to create a Google API key

How to create a Google API key

How to create a Google API key

https://aistudio.google.com/apikey

Browser-use Web-ui

.env file and running the server

  • Here is an example of the .env file
  • You just need to add your API key to the GOOGLE_API_KEY field and you are good to go!
OPENAI_ENDPOINT=https://api.openai.com/v1
OPENAI_API_KEY=

ANTHROPIC_API_KEY=

GOOGLE_API_KEY=XXXXXXXX

AZURE_OPENAI_ENDPOINT=
AZURE_OPENAI_API_KEY=

DEEPSEEK_ENDPOINT=https://api.deepseek.com
DEEPSEEK_API_KEY=
  • To run the server, just type the command below in the web-ui folder:
    • python webui.py --ip 127.0.0.1 --port 7788
    • If you need to install dotenv, you can do it by running uv pip install python-dotenv
    • Then run .venv/bin/python webui.py --ip 127.0.0.1 --port 7788

Browser-use Web-ui in action

Browser-use Web-ui in action

Browser-use Web-ui in action

Browser-use Web-ui in action

Task: go to google.com and find out who won the Oscar for best international film in 2025

Browser-use Web-ui in action

Result: The Guardian article says 'I’m Still Here wins Oscar for best international film, becoming first Brazilian film to do so'

That’s super cool 🤩

What did we learn today?

  • We can run LLMs locally using Ollama and LM Studio
    • Ollama is a command-line tool that allows you to run pre-trained models on your computer
    • LM Studio is a graphic interface that allows you to run any model from Hugging Face
  • We can interact with APIs using Roo Code
    • Roo Code is an extension for VSCode that allows you to interact with many APIs
    • You can use it to run code, write text, and analyse images faster
  • We can use agents to automate tasks
    • Agents are like chatbots that can act autonomously
  • We can browse the internet straight from our code editor
    • Browser-use is a Python library that allows you to interact with websites, extract information, and perform tasks
    • It comes with a web interface that allows you to interact with websites visually

And that’s it for now! 🎉

Thank you for your attention! 😊

>>>>>>> dbcbb5f96eddc5ce0589e633bef7d21b2569a38b:lectures/lecture-15/15-apis.html